package character

import (
	"github.com/spf13/cast"
	"reflect"
)

type Contains interface {
	ContainInterface(arr []interface{}, str interface{}) bool
	ContainString(arr []string, str string) bool
	ContainInt32(arr []int32, str int32) bool
	ContainInt64(arr []int64, str int64) bool
	ContainFloat64(arr []float64, str float64) bool
}

type Container struct {
}

func NewContain() *Container {
	return &Container{}
}

// ContainInterface ...
func (c *Container) ContainInterface(arr interface{}, str interface{}) bool {
	val := reflect.ValueOf(arr)
	if val.Kind() != reflect.Slice {
		return false
	}
	for i := 0; i < val.Len(); i++ {
		elem := val.Index(i)
		// 根据元素类型进行处理
		switch elem.Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			if elem.Int() == cast.ToInt64(str) {
				return true
			}
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
			if elem.Uint() == cast.ToUint64(str) {
				return true
			}
		case reflect.Float32, reflect.Float64:
			if elem.Float() == cast.ToFloat64(str) {
				return true
			}
		case reflect.String:
			if elem.String() == cast.ToString(str) {
				return true
			}
		case reflect.Bool:
			if elem.Bool() == cast.ToBool(str) {
				return true
			}
		default:
			if elem.Interface() == str {
				return true
			}
		}
	}
	return false
}

// ContainString 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainString(arr []string, str string) bool {
	for _, value := range arr {
		if value == str {
			return true // 找到了，返回true
		}
	}
	return false // 遍历完数组都没有找到，返回false
}

// ContainInt32 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainInt32(arr []int32, str int32) bool {
	for _, value := range arr {
		if value == str {
			return true // 找到了，返回true
		}
	}
	return false // 遍历完数组都没有找到，返回false
}

// ContainInt64 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainInt64(arr []int64, str int64) bool {
	for _, value := range arr {
		if value == str {
			return true // 找到了，返回true
		}
	}
	return false // 遍历完数组都没有找到，返回false
}

// ContainFloat64 检查字符串数组中是否包含指定的字符串
func (c *Container) ContainFloat64(arr []float64, str float64) bool {
	for _, value := range arr {
		if value == str {
			return true // 找到了，返回true
		}
	}
	return false // 遍历完数组都没有找到，返回false
}
